package com.m7788.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.m7788.po.UploadInfo;

/**
 * 类名称：httpAsyncTask 类描述： 创建人：huanghsh 创建时间：2011-11-4 上午08:45:57 修改人：huanghsh
 * 修改时间：2011-11-4 上午08:45:57 修改备注：
 */
public class HttpService {

    private static final String BM = "GB2312";
    private static Context context;
    private static HttpService instance;

    private HttpService(Context context) {
        this.context = context;
    }

    public static HttpService getInstance(Context context) {
        if (null == instance) {
            instance = new HttpService(context.getApplicationContext());
        }
        return instance;
    }

    /**
     * 方法名：getDefaultHttpClient 功能：定义httpclient 参数：
     *
     * @return 创建人：huanghsh 创建时间：2011-11-22
     */
    private static DefaultHttpClient getDefaultHttpClient(String userAgent) {
        DefaultHttpClient client = null;
        HttpParams httpParams = new BasicHttpParams();
        // 设置代理
        String host = android.net.Proxy.getDefaultHost();
        int port = android.net.Proxy.getDefaultPort();
        // Log.v(TAG,"代理："+host+"，端口："+port);
        if (host != null) {
            HttpHost httpHost = new HttpHost(host, port);
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
        }
        // 设置超时
        HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
        HttpConnectionParams.setSoTimeout(httpParams, 10000);
        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
        schReg.register(new Scheme("https",
                socketFactory, 443));
        SharedPreferences mSharedPreference = PreferenceManager.getDefaultSharedPreferences(context);
        String ua = mSharedPreference.getString(Constant.UA,"");
        HttpProtocolParams.setUserAgent(httpParams,
                ua);
        // 使用线程安全的连接管理来创建HttpClient
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
                httpParams, schReg);
        client = new DefaultHttpClient(conMgr, httpParams);
        return client;
    }

    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    /**
     * 方法名：checkUpdate 功能：检查更新 参数： 创建人：huanghaishui 创建时间：2013-8-14
     */
    public static String checkUpdate() {
        String result = "";
        // 封装数据
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//
        HttpPost httpPost = new HttpPost(Constant.URL_UPDATE);
        httpPost.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();

        try {
            UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs,
                    "utf-8");
            httpPost.setEntity(p_entity);
            HttpResponse response = client.execute(httpPost);

            result = EntityUtils.toString(response.getEntity(), BM);
            // InputStream content = response.getEntity().getContent();
            // result = convertStreamToString(content);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
            // Log.e("httpReturnUpdate",result);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String qryMenu() {
        String result = "";
        // 封装数据
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//
        HttpPost httpPost = new HttpPost(Constant.URL_MENU);
        httpPost.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();

        try {
            UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs,
                    "utf-8");
            httpPost.setEntity(p_entity);
            HttpResponse response = client.execute(httpPost);

            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String get_top_Menu() {
        String result = "";
        // 封装数据
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//
        HttpPost httpPost = new HttpPost(Constant.URL_TOP_MENU);
        httpPost.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();

        try {
            UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs,
                    "utf-8");
            httpPost.setEntity(p_entity);
            HttpResponse response = client.execute(httpPost);

            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String get_AlbumScreen() {
        String result = "";
        // 封装数据
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//
        HttpPost httpPost = new HttpPost(Constant.URL_ALBUMSCREEN);
        httpPost.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();

        try {
            UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs,
                    "utf-8");
            httpPost.setEntity(p_entity);
            HttpResponse response = client.execute(httpPost);

            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 方法名：login 功能：登录接口 参数：
     *
     * @param usrAgent
     * @param name
     * @param pass1
     * @param pass2
     * @return 创建人：huanghaishui 创建时间：2013-8-14
     */
    public static String login(String name, String pass1, String pass2) {
        String result = "";
        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("username", name));
        params.add(new BasicNameValuePair("psd1", pass1));
        params.add(new BasicNameValuePair("psd2", pass2));
        String param = URLEncodedUtils.format(params, "utf-8");

        HttpGet getMethod = new HttpGet(Constant.URL_LOGIN + "?" + param);
        getMethod.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        DefaultHttpClient httpClient = getDefaultHttpClient(AppUtil
                .getUserAgent(context));

        try {
            HttpResponse response = httpClient.execute(getMethod); // 发起GET请求
            // InputStream content = response.getEntity().getContent();
            // return convertStreamToString(content);
            // response.getEntity()只能读取一次，若读取多次，则转化会出问题
            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 方法名：getPathList 功能：获取用户文件列表 参数：
     *
     * @param usrAgent
     * @param token
     * @return 创建人：huanghaishui 创建时间：2013-8-15
     */
    public static String getPathList() {
        String result = "";
        HttpGet getMethod = new HttpGet(Constant.URL_PATH);
        getMethod.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        DefaultHttpClient httpClient = getDefaultHttpClient(AppUtil
                .getUserAgent(context));

        try {
            HttpResponse response = httpClient.execute(getMethod); // 发起GET请求
            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
            // Log.e("httpReturnPath",result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 方法名：uploadFile 功能：上传到服务器 参数：
     *
     * @param uploadFile
     * @param newName
     * @return 创建人：huanghsh 创建时间：2012-6-7
     */
    public String uploadFile(UploadInfo info) {
        // TODO:未完善的进度方法
        String urlstr = info.getParmasMap().get("url");
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        try {
            // URL url =new URL("http://192.168.1.2:8080/m/I");
            URL url = new URL(urlstr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            /* 允许Input、Output，不使用Cache */
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);

			/* 设置传送的method=POST */
            con.setRequestMethod("POST");
            con.setRequestProperty(Constant.USERAGENT,
                    AppUtil.getUserAgent(context));
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            DataOutputStream ds = new DataOutputStream(con.getOutputStream());
            // 文字说明
            if (info.getParmasMap() != null) {
                Set<String> keys = info.getParmasMap().keySet();
                for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
                    String key = (String) i.next();
                    String value = info.getParmasMap().get(key);

                    ds.writeBytes(twoHyphens + boundary + end);
                    ds.writeBytes("Content-Disposition: form-data; name=\""
                            + key + "\";filename=\"" + key + "\"" + end);
                    ds.writeBytes(end);
                    ds.write(value.getBytes());
                    ds.writeBytes(end);
                }
            }

            // 附件1
            ds.writeBytes(twoHyphens + boundary + end);
            ds.writeBytes("Content-Disposition: form-data; "
                    + "name=\"bigPic\";filename=\"" + new Date().getTime()
                    + ".jpg" + "\"" + end);
            ds.writeBytes("Content-Type: image/jpeg" + end);
            ds.writeBytes(end);

            FileInputStream fStream = new FileInputStream(info.getFile());
            FileInputStream sfStream = new FileInputStream(info.getSmallFile());
            long total = fStream.available() + sfStream.available();
			/* 设置每次写入8192bytes */
            int bufferSize = 8192;
            byte[] buffer = new byte[bufferSize];
            int count = 0;
            int length = 0;
            while ((count = fStream.read(buffer)) != -1) {
                ds.write(buffer, 0, count);
                length += count;
                int a = (int) ((length / (float) total) * 100);
                // Log.e("uuuuu", ""+a);
            }
            ds.writeBytes(end);
            // Log.e("length", ""+length);
            // 附件2
            count = 0;
            ds.writeBytes(twoHyphens + boundary + end);
            ds.writeBytes("Content-Disposition: form-data; "
                    + "name=\"smlPic\";filename=\"" + new Date().getTime()
                    + "_s.jpg" + "\"" + end);
            ds.writeBytes("Content-Type: image/jpeg" + end);
            ds.writeBytes(end);
            while ((count = sfStream.read(buffer)) != -1) {
                ds.write(buffer, 0, count);
                length += count;
                int b = (int) ((length / (float) total) * 100);
                // Log.e("uuuuu", ""+b);
            }
            ds.writeBytes(end);

            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

            fStream.close();
            ds.flush();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    con.getInputStream(), "UTF-8"));
            String line = "";
            String result = "";
            while (null != (line = br.readLine())) {
                result += line;
            }
            ds.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Intent intent = new Intent();
            intent.putExtra("operation", 999);
            intent.setAction(Constant.ACTION_STOA);
            context.sendBroadcast(intent);
            return null;
        }
    }

    /**
     * 方法名：uploadPicture 功能：上传图片 参数：
     *
     * @param usrAgent
     * @param parmas
     * @param uploadFile
     * @return 创建人：huanghaishui 创建时间：2013-8-15
     */
    public static String uploadPicture(String usrAgent,
                                       Map<String, String> parmas, File uploadFile) {
        String result = "1";// 0表示成功 ,1表示失败
        DefaultHttpClient httpclient = getDefaultHttpClient(usrAgent);
        String url = parmas.get("url");
        String cuurimg = parmas.get("cuurimg");
        if (cuurimg != null && cuurimg.length() > 0) {
            int temp = Integer.valueOf(cuurimg);
            temp = temp + 1;
            parmas.put("cuurimg", temp + "");
        }
        // HttpPost httppost = new HttpPost("http://172.16.251.147:8080/m/I2");
        // HttpPost httppost = new HttpPost("http://192.168.1.2:8080/m/I2");
        HttpPost httppost = new HttpPost(url);
        httppost.setHeader(Constant.USERAGENT, usrAgent);

        MultipartEntity mpEntity = new MultipartEntity();
        ContentBody cbFile = new FileBody(uploadFile, "image/jpg");
        try {

            if (parmas != null) {
                Set<String> keys = parmas.keySet();
                for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
                    String key = (String) i.next();
                    ContentBody value = new StringBody(parmas.get(key));
                    mpEntity.addPart(key, value);
                }
            }
            mpEntity.addPart("smlPic", cbFile);// 上传文件
            mpEntity.addPart("bigPic", cbFile);// 上传文件
            httppost.setEntity(mpEntity);
            HttpResponse response;
            response = httpclient.execute(httppost);

            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
            // Log.e("httpReturnUpload",result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        httpclient.getConnectionManager().shutdown();

        return result;
    }

    public String uploadPicture(UploadInfo info) {
        String result = "1";// 0表示成功 ,1表示失败
        DefaultHttpClient httpclient = getDefaultHttpClient(AppUtil
                .getUserAgent(context));
        httpclient.getParams().setIntParameter("http.socket.timeout", 15000);

        String url = info.getParmasMap().get("url");
        // Log.e("inter", ""+url);
        // HttpPost httppost = new HttpPost("http://172.16.251.147:8080/m/I2");
        // HttpPost httppost = new HttpPost("http://192.168.1.2:8080/m/I2");
        if(!TextUtils.isEmpty(url)){
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));

            MultipartEntity mpEntity = new MultipartEntity();
            ContentBody cbFile = new FileBody(info.getFile(), "image/jpg");
            ContentBody cbsFile = new FileBody(info.getSmallFile(), "image/jpg");
            try {

                if (info.getParmasMap() != null) {
                    Set<String> keys = info.getParmasMap().keySet();
                    for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
                        String key = (String) i.next();
                        ContentBody value = new StringBody(info.getParmasMap().get(
                                key));
                        mpEntity.addPart(key, value);
                    }
                }
                mpEntity.addPart("smlPic", cbsFile);// 上传文件
                mpEntity.addPart("bigPic", cbFile);// 上传文件
                httppost.setEntity(mpEntity);
                HttpResponse response;
                response = httpclient.execute(httppost);

                result = EntityUtils.toString(response.getEntity(), BM);
                result = result.replaceAll("\r", "").replaceAll("\n", "");
                result = AppUtil.UnicodeToGB2312(result);
                // Log.e("httpReturnUpload",result);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                return e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return e.getMessage();
            }
            httpclient.getConnectionManager().shutdown();
        }
        return result;
    }

    public static String sendPayResult(String partnerUserId, String orderid) {

        Map<String, String> parmas = new HashMap<String, String>();
        parmas.put("partnerUserId", partnerUserId);
        parmas.put("orderid", orderid);
        String result = "";
        // 封装数据
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//

        String Url = Constant.URL_PAY_SHOPER_RESULT;// 会员u 店主s
        if ("u".equals(partnerUserId.substring(0, 1))) {
            Url = Constant.URL_PAY_USER_RESULT;
        }
        HttpPost httpPost = new HttpPost(Url);

        httpPost.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
        if (parmas != null) {
            Set<String> keys = parmas.keySet();
            for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                pairs.add(new BasicNameValuePair(key, parmas.get(key)));
            }
        }
        try {
            UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs,
                    "utf-8");
            httpPost.setEntity(p_entity);
            HttpResponse response = client.execute(httpPost);

            result = EntityUtils.toString(response.getEntity(), BM);
            result = result.replaceAll("\r", "").replaceAll("\n", "");
            result = AppUtil.UnicodeToGB2312(result);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 上传设备信息
     *
     * @param json
     * @return
     */
    public static String postAppInfo(JSONObject json) {
//		String url = Constant.URL_APP_INFO;
        String url = "http://ttt.7788.com/api/get_app_info.php";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        String result = null;
        try {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String set_bell_time() {
        String url = "http://ttt.7788.com/api/set_bell_time.php";
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        String result = null;
        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(res.getEntity());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String set_https() {
        String url = "http://ttt.7788.com/api/set_https.php";
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
        HttpGet get = new HttpGet(url);
        String result = null;
        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(res.getEntity());
            }
            return result;
        } catch (ClientProtocolException e) {
            result = null;
            e.printStackTrace();
        } catch (IOException e) {
            result = null;
            e.printStackTrace();
        }
        return result;

    }

    public static String isCheckNetWork() {
        String url = "http://ttt.7788.com/api/is_check_network.php";
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
        HttpGet get = new HttpGet(url);
        get.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        String result = null;
        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(res.getEntity());
            }
            return result;
        } catch (ClientProtocolException e) {
            result = null;
            e.printStackTrace();
        } catch (IOException e) {
            result = null;
            e.printStackTrace();
        }
        return result;
    }

    public static String postNetDiagnoseData(JSONObject json) {
        String url = "http://ttt.7788.com/api/check_network_data.php";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        String result = null;
        try {
            StringEntity s = new StringEntity(URLEncoder.encode(json.toString(), "utf-8"));
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String set_start_page() {
        String url = "http://ttt.7788.com/api/set_start_page.php";
        DefaultHttpClient client = getDefaultHttpClient(AppUtil
                .getUserAgent(context));//
        HttpGet get = new HttpGet(url);
        get.setHeader(Constant.USERAGENT, AppUtil.getUserAgent(context));
        String result = null;
        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
